package qlsl.androiddesign.view.subview.commonview;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.qlsl.androiddesign.demo.R;

import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import qlsl.androiddesign.activity.commonactivity.SearchActivity;
import qlsl.androiddesign.adapter.baseadapter.FragmentPagerAdapter;
import qlsl.androiddesign.fragment.basefragment.BaseFragment;
import qlsl.androiddesign.fragment.subfragment.SearchDynamicFragment;
import qlsl.androiddesign.fragment.subfragment.SearchTagFragment;
import qlsl.androiddesign.fragment.subfragment.SearchUserFragment;
import qlsl.androiddesign.http.service.commonservice.SearchService;
import qlsl.androiddesign.listener.GuidePageChangeListener;
import qlsl.androiddesign.listener.OnTextChangeListener;
import qlsl.androiddesign.util.commonutil.Log;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 搜索页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class SearchView extends FunctionView<SearchActivity> {

	private EditText et_search;

	private TextView[] tv_guides = new TextView[3];

	private ImageView iv_cursor;

	private ViewPager viewPager;

	private List<BaseFragment> list;

	private GuidePageChangeListener pageListener;

	public SearchView(SearchActivity activity) {
		super(activity);
		setContentView(R.layout.activity_search);
	}

	protected void initView(View view) {
		hideTitleBar();
		et_search = findViewById(R.id.et_search);
		tv_guides[0] = findViewById(R.id.tv_guide1);
		tv_guides[1] = findViewById(R.id.tv_guide2);
		tv_guides[2] = findViewById(R.id.tv_guide3);
		iv_cursor = findViewById(R.id.iv_cursor);
		viewPager = findViewById(R.id.viewPager);
		viewPager.setOffscreenPageLimit(3);
		setCursorImageMatrix();
	}

	protected void initData() {
		showToast("输入\"测试\"可搜索出测试数据");
	}

	protected void initListener() {
		et_search.addTextChangedListener(textChangeListener);
	}

	public <T> void showData(T... t) {
		if (t[0] instanceof Map) {
			for (BaseFragment fragment : list) {
				fragment.getFunctionView().showData(t);
			}
		}
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_search:
			doClickSearchButton(view);
			break;
		case R.id.tv_guide1:
			doClickGuideView(view);
			break;
		case R.id.tv_guide2:
			doClickGuideView(view);
			break;
		case R.id.tv_guide3:
			doClickGuideView(view);
			break;
		}
	}

	private void setCursorImageMatrix() {
		int bmpW = BitmapFactory.decodeResource(activity.getResources(), R.drawable.iv_cursor).getWidth();
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		float newBmpW = (float) (screenW / tv_guides.length) - 30;
		int offset = (int) (((float) (screenW / tv_guides.length) - newBmpW) / 2);
		Matrix matrix = new Matrix();
		float scale = newBmpW / bmpW;
		matrix.preScale(scale, scale);
		matrix.postTranslate(offset, 0);
		iv_cursor.setImageMatrix(matrix);

		setViewPagerAdapter(offset, (int) newBmpW);
	}

	private void setViewPagerAdapter(int offset, int bmpW) {
		list = new ArrayList<BaseFragment>();
		BaseFragment fragment1 = new SearchDynamicFragment();
		BaseFragment fragment2 = new SearchTagFragment();
		BaseFragment fragment3 = new SearchUserFragment();
		list.add(fragment1);
		list.add(fragment2);
		list.add(fragment3);
		FragmentManager manager = activity.getSupportFragmentManager();
		viewPager.setAdapter(new FragmentPagerAdapter(manager, list));
		viewPager.setCurrentItem(0);
		tv_guides[0].setActivated(true);
		pageListener = new GuidePageChangeListener(tv_guides, iv_cursor, offset, bmpW) {

			public void onPageSelected(int index) {
				super.onPageSelected(index);
			};
		};
		viewPager.setOnPageChangeListener(pageListener);
	}

	private void doClickGuideView(View view) {
		for (int index = 0; index < tv_guides.length; index++) {
			if (view == tv_guides[index]) {
				viewPager.setCurrentItem(index);
				tv_guides[index].setActivated(true);
			} else {
				tv_guides[index].setActivated(false);
			}
		}
	}

	private void doClickSearchButton(View view) {
		Button btn_search = (Button) view;
		String text = btn_search.getText().toString();
		if (text.equals("取消")) {
			activity.finish();
		} else {
			search(0);
		}
	}

	private void search(int page) {
		String keyword = et_search.getText().toString();
		Log.checkDebugModel(activity, keyword);
		SearchService.search(keyword, page, this, activity);
	}

	private OnTextChangeListener textChangeListener = new OnTextChangeListener() {

		public void afterTextChanged(android.text.Editable s) {
			Button btn_search = findViewById(R.id.btn_search);
			if (TextUtils.isEmpty(s)) {
				btn_search.setText("取消");
				btn_search.setActivated(false);
			} else {
				btn_search.setText("搜索");
				btn_search.setActivated(true);
			}
		};
	};

}
